﻿using System.Linq;
using System.Web.Mvc;
using e_TicaretSistemi.Models;

namespace e_TicaretSistemi.Controllers
{
    public class BaseController : Controller
    {
        public Customer CurrentCustomer;
        public static SmDbDataContext Db = new SmDbDataContext();
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            ViewBag.CategoryList = Db.Categories.OrderBy(d => d.OrderNumber).ToList();
            base.OnActionExecuting(filterContext);
            var customer = (Session["LoginKey"] as Customer);
            if (filterContext.ActionDescriptor.ActionName != "Login")
                if (Session["LoginKey"] == null || customer == null)
                {
                    filterContext.Result = RedirectToAction("Login", "Customers");
                    return;
                }
            CurrentCustomer = customer;
        }

        //public static void AddFilter(DefaultFilter filter, IQueryable<Product> list)
        //{
        //    if (filter != null)
        //    {
        //        if (!string.IsNullOrEmpty(filter.SearchString))
        //        {
        //            list = list.Where(d => d.Title.Contains(filter.SearchString) || d.Model.Contains(filter.SearchString) || d.Description.Contains(filter.SearchString));
        //        }
        //        if (filter.SortDir == Enums.SortDirEnum.Asc)
        //        {
        //            if (filter.Sort == Enums.ProductColumnEnum.Title)
        //                list = list.OrderBy(d => d.Title);
        //            if (filter.Sort == Enums.ProductColumnEnum.Price)
        //                list = Db.Products.OrderBy(d => d.Price).AsQueryable();
        //            if (filter.Sort == Enums.ProductColumnEnum.Model)
        //                list = list.OrderBy(d => d.Model);
        //        }
        //        else
        //        {
        //            if (filter.Sort == Enums.ProductColumnEnum.Title)
        //                list.OrderByDescending(d => d.Title);
        //            if (filter.Sort == Enums.ProductColumnEnum.Price)
        //                list.OrderByDescending(p => p.Price).AsQueryable();
        //            if (filter.Sort == Enums.ProductColumnEnum.Model)
        //                list.OrderByDescending(d => d.Model);
        //        }
        //    }
        //}
    }
}